﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace T4Utilities
{
    public class GenericParameterCollection : IEnumerable<string>
    {
        private List<string> _typeNames;

        public GenericParameterCollection()
            : this(new List<string>())
        {
        }

        private GenericParameterCollection(IEnumerable<string> typeNames)
        {
            _typeNames = typeNames as List<string> ?? typeNames.ToList();
        }

        public GenericParameterCollection AllButTheLast()
        {
            return new GenericParameterCollection(_typeNames.Take(_typeNames.Count - 1));
        }

        public void Add(string typeName)
        {
            _typeNames.Add(typeName);
        }

        public void AddRange(string baseName, int amount)
        {
            for (int i = 0; i < amount; i++)
            {
                var suffix = (i + 1).ToString();
                var typeName = baseName + suffix;
                Add(typeName);
            }
        }

        public string TypeDeclaration
        {
            get
            {
                return "<" + string.Join(", ", _typeNames) + ">";
            }
        }
      
        public string MethodsParameters
        {
            get
            {
                return string.Join(", ", _typeNames.Select(type => type + " " + type.ToLower()));
            }
        }

        public override string ToString()
        {
            return TypeDeclaration;
        }

        public IEnumerator<string> GetEnumerator()
        {            
            return _typeNames.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}
